﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DDriver
    {

        /// <summary>
        /// Judge if exists Driver PhoneNumber 
        /// </summary>
        /// <param name="driverPhoneNumber"></param>
        /// <returns>Taxi-id  if return -1 means fail</returns>
        public ArrayList ExistsDriverPhoneNumber(string driverPhoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Taxiid,Name,IMEI,ttaxi.ServerId,Signature from ");
            strSql.Append("tdriver left join ttaxi on Taxiid = ttaxi.id ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)};
            parameters[0].Value = driverPhoneNumber;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                ArrayList al = new ArrayList { ds.Tables[0].Rows[0][0],ds.Tables[0].Rows[0][1], 
                    ds.Tables[0].Rows[0][2], ds.Tables[0].Rows[0][3],ds.Tables[0].Rows[0][4] };
                return al;
            }
        }

        public bool ExistsPhoneNumber(string driverPhoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tdriver");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,20)
             };
            parameters[0].Value = driverPhoneNumber;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }

        public bool UpdateIMEIByPhoneNumber(string driverPhoneNumber,string IMEI)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tdriver set ");
            strSql.Append("IMEI=@IMEI ");
            strSql.Append("WHERE PhoneNumber=@PhoneNumber ");            
            MySqlParameter[] parameters = {
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,15),				 
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)};
            parameters[0].Value = IMEI;
            parameters[1].Value = driverPhoneNumber;            

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }            
            return false;            
        }

        public string GetTaxiNoByPhoneNumber(string phoneNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select taxino from vDriverTaxi ");
            strSql.Append(" where phoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber",  MySqlDbType.VarChar,15)
            };
            parameters[0].Value = phoneNumber;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            string taxino = "";
            if (ds.Tables[0].Rows.Count > 0)
            {

                if (ds.Tables[0].Rows[0]["taxino"] != null && ds.Tables[0].Rows[0]["taxino"].ToString() != "")
                {
                    taxino = ds.Tables[0].Rows[0]["taxino"].ToString();
                }

            }
            return taxino;
        }

        public bool UpdateSignatureByPhoneNumber(string phoneNumber,string signature)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tdriver set ");
            strSql.Append("Signature=@Signature ");
            strSql.Append("WHERE PhoneNumber=@PhoneNumber ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Signature", MySqlDbType.VarChar,20),				 
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)};
            parameters[0].Value = signature;
            parameters[1].Value = phoneNumber;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;            

        }

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "tdriver");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tdriver");
            strSql.Append(" where workid=@workid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@workid", MySqlDbType.VarChar,20)
};
            parameters[0].Value = workid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.DriverInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tdriver(");
            strSql.Append("Name,Gender,PhoneNumber,WorkId,TaxiID,Remark,IMEI)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Gender,@PhoneNumber,@WorkId,@TaxiID,@Remark,@IMEI)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar,45),
					new MySqlParameter("@Gender", MySqlDbType.VarChar,2),
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@WorkId", MySqlDbType.VarChar,20),
					new MySqlParameter("@TaxiID", MySqlDbType.Int32,10),
					new MySqlParameter("@Remark", MySqlDbType.Text),
                    new MySqlParameter("@IMEI", MySqlDbType.VarChar,15)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Gender;
            parameters[2].Value = model.PhoneNumber;
            parameters[3].Value = model.WorkId;
            parameters[4].Value = model.TaxiID;
            parameters[5].Value = model.Remark;
            parameters[6].Value = model.IMEI;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.DriverInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tdriver set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Gender=@Gender,");
            strSql.Append("PhoneNumber=@PhoneNumber,");
            strSql.Append("WorkId=@WorkId,");
            strSql.Append("TaxiID=@TaxiID,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("IMEI=@IMEI");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar,45),
					new MySqlParameter("@Gender", MySqlDbType.VarChar,2),
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@WorkId", MySqlDbType.VarChar,20),
					new MySqlParameter("@TaxiID", MySqlDbType.Int32,10),
					new MySqlParameter("@Remark", MySqlDbType.Text),
                    new MySqlParameter("@IMEI", MySqlDbType.VarChar,15),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Gender;
            parameters[2].Value = model.PhoneNumber;
            parameters[3].Value = model.WorkId;
            parameters[4].Value = model.TaxiID;
            parameters[5].Value = model.Remark;
            parameters[6].Value = model.IMEI;
            parameters[7].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新指定号码评分总数
        /// </summary>
        public bool UpdateStarLevel(int starLevel, string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tdriver set ");
            strSql.Append("StarLevelsum=StarLevelsum+" + starLevel+",");
            strSql.Append("Votecount=Votecount+1 where ");
            strSql.Append("PhoneNumber=@PhoneNumber");            
            MySqlParameter[] parameters = {										
					//new MySqlParameter("@Name", MySqlDbType.Int32,10),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)};
            //parameters[0].Value = starLevel;
            parameters[0].Value = phoneNumber;           
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public int GetDriverStar(string phoneNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select StarLevelsum,Votecount from tdriver ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber",  MySqlDbType.VarChar,15)
            };
            parameters[0].Value = phoneNumber;            
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            int star = 0;
            int starlevelsum = 0;
            int votecount = 0;
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["StarLevelsum"] != null && ds.Tables[0].Rows[0]["StarLevelsum"].ToString() != "")
                {
                    starlevelsum = int.Parse(ds.Tables[0].Rows[0]["StarLevelsum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Votecount"] != null && ds.Tables[0].Rows[0]["Votecount"].ToString() != "")
                {
                    votecount = int.Parse(ds.Tables[0].Rows[0]["Votecount"].ToString());
                }
                if(votecount>0 && starlevelsum>0)
                {
                    star = starlevelsum/votecount;
                    //Math.Round(Convert.ToDouble(txtnum), 2, MidpointRounding.AwayFromZero);
                }
            }
            return star;
        }

       

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tdriver ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tdriver ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.DriverInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,Name,Gender,PhoneNumber,WorkId,TaxiID,Remark from tdriver ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.DriverInfo model = new Model.DriverInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Gender"] != null && ds.Tables[0].Rows[0]["Gender"].ToString() != "")
                {
                    model.Gender = ds.Tables[0].Rows[0]["Gender"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["WorkId"] != null && ds.Tables[0].Rows[0]["WorkId"].ToString() != "")
                {
                    model.WorkId = ds.Tables[0].Rows[0]["WorkId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TaxiID"] != null && ds.Tables[0].Rows[0]["TaxiID"].ToString() != "")
                {
                    model.TaxiID = int.Parse(ds.Tables[0].Rows[0]["TaxiID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select   id,name,phoneNumber,IMEI,workid,taxiid,remark,tremark,taxino,taxitype, ");
            //strSql.Append("(CASE gender WHEN 0 THEN '女' WHEN 1 THEN '男' END) AS gender");
            //strSql.Append(" FROM vDriverTaxi ");
            //if (strWhere.Trim() != "")
            //{
            //    strSql.Append(" where " + strWhere);
            //}

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT t.id,t.name,t.phoneNumber,");
            strSql.Append("(CASE t.Gender WHEN 0 THEN '女' WHEN 1 THEN '男' END) AS gender,");
            strSql.Append("t.workid,t.taxiid,t.remark,t.IMEI,");
            strSql.Append("ttaxi.taxino,ttaxi.taxitype,tcompany.CompanyName ");
            strSql.Append("FROM taxidispatchsystem.tdriver t ");
            strSql.Append("left join ttaxi on t.TaxiID=ttaxi.id ");
            strSql.Append("left join tcompany on ttaxi.CompanyId = tcompany.Id ");            
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperMySQL.Query(strSql.ToString());
        }

        public DataSet GetList(string parameter, string parameterVaule)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select   id,name,phoneNumber,IMEI,workid,taxiid,remark,tremark,taxino,taxitype, ");
            //strSql.Append("(CASE gender WHEN 0 THEN '女' WHEN 1 THEN '男' END) AS gender");
            //strSql.Append(" FROM vDriverTaxi ");
            //strSql.Append(" where ");
            //strSql.Append(parameter);
            //strSql.Append(" like @A");
            //MySqlParameter[] parameters = {
            //                                  new MySqlParameter("@A", MySqlDbType.Text)
            //                              };
            //parameters[0].Value = "%" + parameterVaule + "%";

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT t.id,t.name,t.gender,t.phoneNumber,");
            strSql.Append("t.workid,t.taxiid,t.remark,t.IMEI,");
            strSql.Append("ttaxi.taxino,tcompany.CompanyName ");
            strSql.Append("FROM taxidispatchsystem.tdriver t ");
            strSql.Append("left join ttaxi on t.TaxiID=ttaxi.id ");
            strSql.Append("left join tcompany on ttaxi.CompanyId = tcompany.Id ");
            strSql.Append(" where ");
            strSql.Append(parameter);
            strSql.Append(" like @A");
            MySqlParameter[] parameters = {
                                              new MySqlParameter("@A", MySqlDbType.Text)
                                          };
            parameters[0].Value = "%" + parameterVaule + "%";

            return DbHelperMySQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tdriver ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from tdriver T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tdriver";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
